# -*- coding: utf-8 -*-
#coding = utf-8

'''
Created on July 25, 2013

@author: Tong Zhang

the manager operation for the data in the redis
'''

from redis_manager import RedisManage
import datetime
import uuid
import logging


class RedisOperation:

	@staticmethod
	def get_current_deviceid():
		logger = logging.getLogger('et.kernel.tcp.RedisOperation.get_current_deviceid')

		try:

			current_deviceid = RedisManage.get_object_to_str('current_deviceid')
			return current_deviceid
		except Exception, e:
			logger.error(e)
			return ''.join(['\xff','\xff','\xff','\xff','\xff','\xff'])

	@staticmethod
	def set_current_deviceid(device):

		logger = logging.getLogger('et.kernel.tcp.RedisOperation.set_current_deviceid')

		try:

			RedisManage.add_object_to_str('current_deviceid',device)
			return True
		except Exception, e:
			logger.error(e)
			return False

	@staticmethod
	def set_all_deviceids(device):

		logger = logging.getLogger('et.kernel.tcp.RedisOperation.set_all_deviceids')

		try:

			RedisManage.add_object_to_sets('all_deviceids',device)
			return True
		except Exception, e:
			logger.error(e)
			return False

	@staticmethod
	def get_deviceid():

		logger = logging.getLogger('et.kernel.tcp.RedisOperation.get_deviceid')

		try:
			if RedisManage.check_exists_key('transcribe_deviceid'):
				deviceid = RedisManage.get_object_to_str('transcribe_deviceid')
				RedisManage.del_active_key('transcribe_deviceid')
				return deviceid
			else:
				return ''.join(['\xff','\xff','\xff','\xff','\xff','\xff'])
		except Exception, e:
			logger.error(e)
			return ''.join(['\xff','\xff','\xff','\xff','\xff','\xff'])

	@staticmethod
	def check_device_bind(device):

		logger = logging.getLogger('et.kernel.tcp.RedisOperation.check_device_bind')

		try:
			if RedisManage.check_exists_key('bind_devices'):
				if RedisManage.check_object_from_sets('bind_devices',device):
					return True
			return False
		except Exception, e:
			logger.error(e)
			return False

	@staticmethod
	def get_userid_by_device(device):

		logger = logging.getLogger('et.kernel.tcp.RedisOperation.check_device_bind')

		try:
			if RedisManage.check_exists_key('device:'+str(device)+':userid'):
				return int(RedisManage.get_object_to_str('device:'+str(device)+':userid'))
			return 0
		except Exception, e:
			logger.error(e)
			return 0

	@staticmethod
	def set_device_by_ip(ip, device, time = 300):

		logger = logging.getLogger('et.kernel.tcp.RedisOperation.set_device_by_ip')

		try:
			if ip == '219.234.181.213':
				ip = '219.234.181.218'
			return RedisManage.append_object_to_list('ip:'+str(ip)+':deviceid', device, 'l', time)
		except Exception, e:
			logger.error(e)
			return False

	@staticmethod
	def set_gateway_by_ip(ip, time = 45):

		logger = logging.getLogger('et.kernel.tcp.RedisOperation.set_gateway_by_ip')

		try:
			if ip == '219.234.181.213':
				ip = '219.234.181.218'
			return RedisManage.append_object_to_list('ip:'+str(ip)+':gateway', 1, 'l', time)
		except Exception, e:
			logger.error(e)
			return False

	@staticmethod
	def get_device_info(deviceid):

		logger = logging.getLogger('et.kernel.tcp.RedisOperation.get_device_info')

		device_detail = {}

		try:
			if RedisManage.check_exists_key('device:'+str(deviceid)+':update'):
				logger.debug('lol_redis')

				device_detail['height'] = RedisManage.get_object_to_str('device:'+str(deviceid)+':height')
				device_detail['weight'] = RedisManage.get_object_to_str('device:'+str(deviceid)+':weight')
				device_detail['age'] = RedisManage.get_object_to_str('device:'+str(deviceid)+':age')
				device_detail['gender'] = RedisManage.get_object_to_str('device:'+str(deviceid)+':gender')
				device_detail['user_goal'] = RedisManage.get_object_to_str('device:'+str(deviceid)+':user_goal')
				device_detail['sleep_start'] = RedisManage.get_object_to_str('device:'+str(deviceid)+':sleep_start')
				device_detail['sleep_end'] = RedisManage.get_object_to_str('device:'+str(deviceid)+':sleep_end')
				device_detail['auto_time'] = RedisManage.get_object_to_str('device:'+str(deviceid)+':auto_time')

				logger.debug(device_detail) 
				return device_detail

			logger.debug('no device detail dict')
			return {}

		except Exception, e:
			logger.error(e)
			return {}

	@staticmethod
	def get_device_weight(deviceid):

		logger = logging.getLogger('')
		weight = 70
		
		try:

			if RedisManage.check_exists_key('device:'+str(deviceid)+':weight'):
				weight = RedisManage.get_object_to_str('device:'+str(deviceid)+':weight')

			return int(float(weight))

		except Exception, e:
			logger.error(e)
			return weight
